﻿using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class Game : MonoBehaviour
{
    public enum Game_Status
    {
        Ready, Start, Over
    }
    #region UI显示
    [Header("UI显示")]
    public GameObject PanelReady;
    public GameObject PanelStart;
    public GameObject PanelOver;
    [SerializeField][Tooltip("血条")] public Slider HP;
    public Image HPColor;
    [SerializeField][Tooltip("游戏开始后显示分数")] TextMeshProUGUI ScoreText;
    [SerializeField][Tooltip("游戏结束后显示分数")] TextMeshProUGUI OverText;
    [SerializeField][Tooltip("游戏最高分数")] TextMeshProUGUI MaxText;
    #endregion

    [Header("其他")]
    public BuildManager buildManager;
    public UnitManager unitManager;
    [SerializeField][Tooltip("player对象")] private Player player;

    [SerializeField]
    Game_Status Status;
    public Game_Status status
    {
        get
        {
            return Status;
        }
        private set
        {
            this.Status = value;
            UpdateUI();
        }
    }


    private int UIScrore;
    private int MaxScore = 0;

    public int score
    {
        get
        {
            return UIScrore;
        }
        set
        {
            UIScrore = value;
            ScoreText.text = value.ToString();
        }
    }
    private void Player_OnDeath()
    {
        status = Game_Status.Over;
        MaxScore = Mathf.Max(MaxScore, score);
        MaxText.text = MaxScore.ToString();
        OverText.text = score.ToString();

        unitManager.EndRun();
        buildManager.EndRun();
    }

    private void Player_OnScore(int v)
    {
        score += v;
    }

    public void StartGame()
    {
        status = Game_Status.Start;
        buildManager.StartRun();
        unitManager.StartRun();
        player.Fly();
    }

    public void Restart()
    {
        Init();
        buildManager.Init();
        unitManager.Init();
        player.Init();
        player.Idle();
    }

    private void Init()
    {
        status = Game_Status.Ready;
        score = 0;
    }


    public void UpdateUI()
    {
        PanelStart.SetActive(status == Game_Status.Start);
        PanelOver.SetActive(status == Game_Status.Over);
        PanelReady.SetActive(status == Game_Status.Ready);
    }

    // Start is called before the first frame update
    void Start()
    {
        status = Game_Status.Ready;
        player.OnDeath += Player_OnDeath;
        player.OnScore = Player_OnScore;
        HPColor = HP.transform.GetChild(2).GetChild(0).gameObject.GetComponent<Image>();

    }

    Color yColor = new Color(0.8773585f, 0.717618f, 0.07780345f);
    Color gColor = new Color(0.1245677f, 0.4622642f, 0.08024208f);
    private void Update()
    {
        HP.value = Mathf.Lerp(HP.value, player.HP, 0.05f);

        if (player.HP < 700)
        {
            if (player.HP > 400)
            {
                HPColor.color = Color.Lerp(HPColor.color, yColor, 0.05f);
            }
            else
            {
                HPColor.color = Color.Lerp(HPColor.color, Color.red, 0.05f);
            }
        }
        else
        {
            HPColor.color = Color.Lerp(HPColor.color, gColor, 0.1f);
        }
    }
}
